#[macro_use] extern crate fluffy;
#[macro_use] extern crate lazy_static;

use actix_web::{HttpServer, App, middleware, web};
use fluffy::{db};

mod models;
mod caches;
mod controllers;
mod config;

use controllers::{index, videos};

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
        
    let host_port = format!("{}:{}", config::HOST, config::PORT); //地址:端口
    println!("监听地址: http://{}", host_port);
    let conn_string = config::get_conn_string(); //数据库相关信息
    db::init_connections(&conn_string);

    std::env::set_var("RUST_LOG", "actix_web=info");
    env_logger::init();

    HttpServer::new(|| {

        let tpl = tmpl!("/templates/**/*"); //模板引擎

        App::new()
            //.wrap(CookieSession::signed(&[0; 32]).secure(false))
            .data(tpl)
            .wrap(middleware::Logger::default()) // enable logger
            .service(web::resource("/test").to(index::test))
            .service(get!("/", index::index))
            .service(get!("/index", index::index))
            .service(get!("/index/index", index::index))
            .service(get!("/contact", index::contact))
            .service(get!("/about", index::about))
            .service(get!("/feedback", index::feedback))
            .service(get!("/videos", videos::index))
            .service(get!("/videos/view", videos::view))
    })
    .bind(host_port)?
    .run()
    .await
}
